package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.mainframeSub;

import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.Workorder;

public class RFCDisplayTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String[] TABLECOLUMNNAMES = { "ID", "Status", "Datum",
			"Titel" };
	private final int idWith, stateWidth, dateWidth;
	private DefaultTableModel tableModel;

	private ArrayList<RFC> rfcs;
	private ArrayList<Workorder> workOrders;

	public RFCDisplayTable() {
		
		this.idWith = 40;
		this.stateWidth = 80;
		this.dateWidth = 150;
		this.rfcs = new ArrayList<RFC>();
		this.workOrders = new ArrayList<Workorder>();
		
		this.setModel(this.setupTablemodel());
		this.setupColums();

	}
	
	public RFCDisplayTable(boolean forworkOrders){
		
		this.idWith = 40;
		this.stateWidth = 80;
		this.dateWidth = 150;
		this.rfcs = new ArrayList<RFC>();
		this.workOrders = new ArrayList<Workorder>();
		String[] colums = {"ID","Status","Start"," Titel"};
		this.setModel(this.setupWoTablemodel(colums));
		this.setupColums();
	}

	public boolean isCellEditable(int x, int y) {
		return false;
	}

	private void setupColums() {

		this.getColumnModel().getColumn(0).setMaxWidth(this.idWith);
		this.getColumnModel().getColumn(1).setMaxWidth(this.stateWidth);
		this.getColumnModel().getColumn(2).setMaxWidth(this.dateWidth);
		this.getColumnModel().getColumn(0).setMinWidth(this.idWith - 1);
		this.getColumnModel().getColumn(1).setMinWidth(this.stateWidth - 1);
		this.getColumnModel().getColumn(2).setMinWidth(this.dateWidth - 1);

	}

	private TableModel setupTablemodel() {
		Object[][] data = null;
		this.tableModel = new DefaultTableModel(data, TABLECOLUMNNAMES);
		return tableModel;
		
	}
	private TableModel setupWoTablemodel(String[] colums) {
		Object[][] data = null;
		this.tableModel = new DefaultTableModel(data, colums);
		return tableModel;
	}

	public void clearTable() {

		while (this.tableModel.getRowCount() > 0) {
			for (int i = 0; i < this.tableModel.getRowCount(); i++) {
				this.tableModel.removeRow(i);
				if (!(this.rfcs.size()==0)) {					
					this.rfcs.remove(i);
				}
				if(!(this.workOrders.size()==0)){
					this.workOrders.remove(i);
				}
			}
		}


	}

	public void addRFCinTable(RFC rfc) {
		this.rfcs.add(rfc);
		String[] rfcData = new String[4];
		rfcData[0] = rfc.getIdRFC();
		rfcData[1] = rfc.getStatus();
		rfcData[2] = rfc.getSubmission_Date();
		rfcData[3] = rfc.getSummary();

		this.tableModel.addRow(rfcData);
		this.setEnabled(true);
	}
	
	public void addWorkOrderInTable(Workorder workorder) {
		this.workOrders.add(workorder);
		String[] wo = new String[4];
		wo[0] = workorder.getWo_id();
		wo[1] = workorder.getWo_status();
		wo[2] = workorder.getPlanned_start();
		wo[3] = workorder.getWo_title();
		
		this.tableModel.addRow(wo);
		this.setEnabled(true);
	}
	
	public void displayNoElenemt() {
		String[] noData = new String[4];
		noData[0] = "*";
		noData[1] = "*";
		noData[2] = "*";
		noData[3] = "Kein Eintrag gefunden";
		this.tableModel.addRow(noData);
		this.setEnabled(false);
	}

	public RFC getSelectedRFCinTable() {
		if (this.getSelectedRow()>=0 && this.rfcs.size()>0) {
			
			return this.rfcs.get(this.getSelectedRow());
			
		}else {
			return null;
		}
	}
	
	public Workorder getSelectedWoinTable() {
		if (this.getSelectedRow()>=0 && this.workOrders.size()>0) {
			
			return this.workOrders.get(this.getSelectedRow());
		}else {
			return null;
		}
	}

	public RFC getElementatRow(int i) {
		return rfcs.get(i);
	}
}
